package sicau.carManager.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import sicau.carManager.entity.ViolationInformation;
import sicau.carManager.mapper.ViolationInformationMapper;
import sicau.carManager.service.ViolationInformationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.File;
import java.time.LocalDateTime;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Predicate;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ITcz
 * @since 2021-02-05
 */
@Service
public class ViolationInformationServiceImpl extends ServiceImpl<ViolationInformationMapper, ViolationInformation> implements ViolationInformationService {

    @Autowired
    ViolationInformationMapper vioMapper;

    @Value("${ip.imagePath}")
    String imagePath;

    @Override
    public Integer selectViolationNumByPlateNum(String plateNum, String campus) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("license_plate_number", plateNum);
        queryWrapper.eq("campus", campus);
        return vioMapper.selectCount(queryWrapper);
    }

    @Override
    public List<ViolationInformation> selectListByTime(LocalDateTime beginDay, LocalDateTime endDay, String campus) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.between("time", beginDay, endDay);
        queryWrapper.eq("campus", campus);
        return vioMapper.selectList(queryWrapper);
    }

    @Override
    public void cleanMoreThanOneYearViolationInformation() {
        QueryWrapper queryWrapper = new QueryWrapper();
        //获取当前时间，精确到天
        LocalDateTime now = LocalDateTime.now().withNano(0).withSecond(0).withMinute(0).withHour(0);
        //获取一年前的时间，精确到天
        LocalDateTime oneYearAge = LocalDateTime.now().minusYears(1).withNano(0).withSecond(0).withMinute(0).withHour(0);
        //删除一年以前的违章信息
        queryWrapper.notBetween("time", oneYearAge, now);

        //查询出所有的过期图片你的url，删除本地图片
        List<ViolationInformation> list = vioMapper.selectList(queryWrapper);
        for (ViolationInformation violationInformation : list) {
            String pathPrefix = imagePath;
            String imageName = violationInformation.getImageName();
            File imgFile = new File(pathPrefix + imageName);
            imgFile.delete();
        }

        vioMapper.delete(queryWrapper);
    }

    @Override
    public List<ViolationInformation> selectListByLicensePlate(String licensePlate, String campus) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("campus", campus);
        queryWrapper.eq("license_plate_number", licensePlate);
        //只查询这六个字段
        queryWrapper.select("license_plate_number", "phone_number", "time", "place", "reason", "image_url");
        return vioMapper.selectList(queryWrapper);
    }

    @Override
    public List<ViolationInformation> selectListThanThreeTimes(String campus) {
        List<ViolationInformation> resList = new LinkedList<ViolationInformation>();

        //判空
        if (resList.isEmpty()) {
            return null;
        }
        //1）、获得违章次数达到3次及其以上的车牌号，并放入HashSet集合中
        List<String> list = vioMapper.selectLicensePlateThanThreeTimes(campus);
        //2）、根据车牌号去获得所有的违章信息，并放入resList中返回
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("campus", campus);
        queryWrapper.in("license_plate_number", list);
        queryWrapper.select("id","license_plate_number","phone_number","time","place","reason","image_url");
        resList = vioMapper.selectList(queryWrapper);
        return resList;
    }

    @Override
    public void delete(ViolationInformation violationInformation) {

        String imageName = violationInformation.getImageName();
        Integer id = violationInformation.getId();

        File file = new File(imagePath + imageName);
        if (file.exists()) {
            try {
                file.delete();
                vioMapper.deleteById(id);
            }catch (Exception ignored) {
            }
        }

    }


}
